﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

public class InteractiveItems : MonoBehaviour
{
    [SerializeField] private bool DoInteractive = false;
    [SerializeField] protected bool InteractiveTip = true;
    [SerializeField] protected float tipLightSpeed = 1;
    [SerializeField] private float coolingTime = 0.2f;
    [SerializeField] private bool birdCan = true;
    [SerializeField] private bool boyCan = true;


    protected Light2D light;
    private float inten;
    private bool offing = false;
    private bool oning = false;

    #region 冷却
    private float coolingTime_wait = 0;
    private bool inCooling = false;
    #endregion


    protected virtual void Awake()
    {
        if (InteractiveTip)
        {
            light = GetComponent<Light2D>();
            if (light != null)
            {
                inten = light.intensity;
                light.intensity = 0;
            }
            else
            {
                InteractiveTip = false;
            }

        }

    }


    public void Interactive_Interface(bool isBird = false, bool isObj = false)
    {
        if (!isObj)
        {
            if (isBird && !birdCan)
            {
                return;
            }
            if (!isBird && !boyCan)
            {
                return;
            }
        }
        if (!inCooling)
        {
            Interactive();
            inCooling = true;
        }
    }

    public virtual void Interactive()
    {

    }

    protected virtual void Update()
    {
        if (DoInteractive)
        {
            DoInteractive = false;
            Interactive();
        }
        if (inCooling)
        {
            coolingTime_wait += Time.deltaTime;
            if (coolingTime_wait >= coolingTime)
            {
                coolingTime_wait = 0;
                inCooling = false;
            }
        }
    }

    protected virtual void OnTriggerStay2D(Collider2D collision)
    {
        if (
            CanDoThis(collision) &&
            InteractiveTip &&
            !oning
           )
        {
            if (offing)
            {
                offing = false;
            }

            if (light.intensity < inten)
            {
                light.intensity += Time.fixedDeltaTime * tipLightSpeed;
                if (light.intensity >= inten)
                {
                    light.intensity = inten;
                    oning = true;
                }
            }
        }
    }

    protected virtual void OnTriggerExit2D(Collider2D collision)
    {
        if ( CanDoThis(collision) && InteractiveTip)
        {
            offing = true;
            oning = false;
        }
    }

    protected virtual void FixedUpdate()
    {
        if (InteractiveTip && offing)
        {
            light.intensity -= Time.fixedDeltaTime * tipLightSpeed;
            if (light.intensity <= 0)
            {
                light.intensity = 0;
                offing = false;
            }
        }
    }


    private bool CanDoThis(Collider2D collision)
    {
        return (collision.CompareTag(TagAndLayersConfigs.Tag.Player) && boyCan)
            ||
            (collision.CompareTag(TagAndLayersConfigs.Tag.Bird) && birdCan);
    }

}




